package com.example.bughuangkai.controller;

import com.example.bughuangkai.entity.Bug;
import com.example.bughuangkai.service.BugService;
import com.example.bughuangkai.vo.ReturnT;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄凯
 * @description bug
 * @date 2022-02-12
 */
@Controller
public class BugController {

    @Autowired
    private BugService bugService;

    /**
     * 新增
     *
     * @author 黄凯
     * @date 2022/02/12
     **/
    @RequestMapping("/bug/insert")
    public Object insert(Bug bug) {
        return bugService.insert(bug);
    }

    /**
     * 刪除
     *
     * @author 黄凯
     * @date 2022/02/12
     **/
    @RequestMapping("/bug/delete")
    public ReturnT<String> delete(int id) {
        return bugService.delete(id);
    }

    /**
     * 更新
     *
     * @author 黄凯
     * @date 2022/02/12
     **/
    @RequestMapping("/bug/update")
    public ReturnT<String> update(Bug bug) {
        return bugService.update(bug);
    }

    /**
     * 查询 根据主键 id 查询
     *
     * @author 黄凯
     * @date 2022/02/12
     **/
    @RequestMapping("/bug/load")
    public Object load(int id) {
        return bugService.load(id);
    }

    /**
     * 查询 分页查询
     *
     * @author 黄凯
     * @date 2022/02/12
     **/
    @GetMapping("/bug/pageList")
    public String pageList(@RequestParam(required = false, defaultValue = "0") int pageNum,
                           @RequestParam(required = false, defaultValue = "10") int pageSize,
                           Model model) {

        Page page = new Page(pageNum,pageSize);
        PageInfo<Bug> pageInfo = bugService.pageList(page);
        model.addAttribute("pageInfo", pageInfo);

        List<Bug> list = new ArrayList<Bug>();
        Bug bug = new Bug();
        bug.setName("666");
        list.add(bug);
        model.addAttribute("list1", list);
        return "bugList";
    }

}